package com.formiaomiao.demain.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.model.DemainBean;
import com.formiaomiao.demain.model.FollowAllBean;
import com.formiaomiao.demain.util.DateUtils;
import com.formiaomiao.demain.util.UseGlideUtils;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;

import java.util.ArrayList;
import java.util.List;

public class FollowDataRecycleAdapter extends RecyclerView.Adapter<FollowDataRecycleAdapter.ViewHolder> {

    Context context;
    List<FollowAllBean.DataBean.ListBean.DemainListBean> list;

    public onLongItemClickListener onLongItemClickListener;

    public interface onLongItemClickListener {
        void longItemClickListener(View v, int i);

        void headItemClickListener(View v, int i);

        void shareItemClickListener(View v, int i);

        void msgItemClickListener(View v, int i);

        void likeItemClickListener(View v, int i);
    }

    public void setOnLongItemClickListener(onLongItemClickListener onLongItemClickListener) {
        this.onLongItemClickListener = onLongItemClickListener;
    }

    public FollowDataRecycleAdapter(Context context, List<FollowAllBean.DataBean.ListBean.DemainListBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.demain_item, parent, false);
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        UseGlideUtils.setGlide(context, list.get(position).getUserBean().getUserHead(), holder.iv_userhead);
        holder.tv_username.setText(list.get(position).getFriendremarks() == null || list.get(position).getFriendremarks().equals("")
                ? list.get(position).getUserBean().getNickName() : list.get(position).getFriendremarks());
        holder.tv_content.setText(list.get(position).getContent());
        holder.tv_time.setText(DateUtils.timess(Long.parseLong(list.get(position).getContentDate())));
        holder.tv_address.setText(list.get(position).getAddress());
        holder.tv_msg.setText(String.valueOf(list.get(position).getMsgcount()));
        holder.tv_like.setText(String.valueOf(list.get(position).getLikecount()));
        if (list.get(position).getIslike() == 0) {
            holder.iv_like.setImageResource(R.mipmap.like);
        } else {
            holder.iv_like.setImageResource(R.mipmap.liked);
        }
        ArrayList<ImageInfo> imageInfo = new ArrayList<>();
        List<FollowAllBean.DataBean.ListBean.DemainListBean.ImgBeanBean> imageDetails = list.get(position).getImgBean();
        if (imageDetails != null) {
            for (FollowAllBean.DataBean.ListBean.DemainListBean.ImgBeanBean imageDetail : imageDetails) {
                ImageInfo info = new ImageInfo();
                info.setThumbnailUrl(imageDetail.getUrl());
                info.setBigImageUrl(imageDetail.getUrl());
                imageInfo.add(info);
            }
        }
        holder.ng_imgs.setAdapter(new NineGridViewClickAdapter(context, imageInfo));

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                onLongItemClickListener.longItemClickListener(v, position);
                return true;
            }
        });

        holder.iv_userhead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onLongItemClickListener.headItemClickListener(view, position);
            }
        });

        holder.tv_username.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onLongItemClickListener.headItemClickListener(view, position);
            }
        });

        holder.iv_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onLongItemClickListener.shareItemClickListener(view, position);
            }
        });

        holder.ll_msg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onLongItemClickListener.msgItemClickListener(view, position);
            }
        });

        holder.ll_like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onLongItemClickListener.likeItemClickListener(view, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_userhead, iv_share, iv_like;
        TextView tv_username, tv_content, tv_time, tv_address, tv_msg, tv_like;
        NineGridView ng_imgs;
        LinearLayout ll_msg, ll_like;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_userhead = itemView.findViewById(R.id.iv_userhead);
            iv_share = itemView.findViewById(R.id.iv_share);
            iv_like = itemView.findViewById(R.id.iv_like);
            tv_username = itemView.findViewById(R.id.tv_username);
            tv_content = itemView.findViewById(R.id.tv_content);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_address = itemView.findViewById(R.id.tv_address);
            tv_msg = itemView.findViewById(R.id.tv_msg);
            tv_like = itemView.findViewById(R.id.tv_like);
            ng_imgs = itemView.findViewById(R.id.ng_imgs);
            ll_msg = itemView.findViewById(R.id.ll_msg);
            ll_like = itemView.findViewById(R.id.ll_like);
        }
    }

}
